home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / PowerPC / vbcc / machines / amigawos / libsrc / time / strftime.c < prev    next >
C/C++ Source or Header  |  1998-08-02  |  3KB  |  126 lines

  1. #include <time.h>
  2. #include <libraries/locale.h>
  3.  
  4. #define ADDS(st)  tmp=strftime(s,maxsize-size,(st),timeptr);break;
  5.  
  6. #define ADDN(a,b) tmp=strfnumb(s,maxsize-size,(a),(b));break;
  7.  
  8. #define STOR(c)   if(++size<=maxsize)*s++=(c);
  9.  
  10. #define STR(a) (strings[(a)-1])
  11.  
  12.  
  13. /* All calendar strings */
  14. static const char *strings[]=
  15. { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",
  16.   "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
  17.   "January","February","March","April","May","June",
  18.   "July","August","September","October","November","December",
  19.   "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",
  20.   "","","AM","PM" };
  21.  
  22. static size_t strfnumb(char *s,size_t maxsize,signed int places,size_t value)
  23. { size_t size=0;
  24.   if(places>1)
  25.     size=strfnumb(s,maxsize,places-1,value/10);
  26.   else if(value>=10)
  27.     size=strfnumb(s,maxsize,places+1,value/10);
  28.   else
  29.     while ((places++<-1) && (++size<=maxsize)) s[size-1]=' ';
  30.   if(++size<=maxsize)
  31.     s[size-1]=(value%10+'0');
  32.   return size;
  33. }
  34.  
  35. size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr)
  36. { size_t size=0,tmp;
  37.   while(*format)
  38.   { if(*format=='%')
  39.     { tmp=0;
  40.       switch(*++format)
  41.       { case 'a':
  42.           ADDS(STR(ABDAY_1+timeptr->tm_wday));
  43.         case 'b':
  44.         case 'h':
  45.           ADDS(STR(ABMON_1+timeptr->tm_mon));
  46.         case 'c':
  47.           ADDS("%m/%d/%y");
  48.         case 'd':
  49.           ADDN(2,timeptr->tm_mday);
  50.         case 'e':
  51.           ADDN(-2,timeptr->tm_mday);
  52.         case 'j':
  53.           ADDN(3,timeptr->tm_yday+1);
  54.         case 'k':
  55.           ADDN(-2,timeptr->tm_hour);
  56.         case 'l':
  57.           ADDN(-2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
  58.         case 'm':
  59.           ADDN(2,timeptr->tm_mon+1);
  60.         case 'p':
  61.           ADDS(STR(AM_STR+(timeptr->tm_hour>=12)));
  62.         case 'r':
  63.           ADDS("%I:%M:%S %p");
  64.         case 'w':
  65.           ADDN(1,timeptr->tm_wday);
  66.         case 'x':
  67.           ADDS("%m/%d/%y %H:%M:%S");
  68.         case 'y':
  69.           ADDN(2,timeptr->tm_year%100);
  70.         case 'A':
  71.           ADDS(STR(DAY_1+timeptr->tm_wday));
  72.         case 'B':
  73.           ADDS(STR(MON_1+timeptr->tm_mon));
  74.         case 'C':
  75.           ADDS("%a %b %e %H:%M:%S %Y");
  76.         case 'D':
  77.           ADDS("%m/%d/%y");
  78.         case 'H':
  79.           ADDN(2,timeptr->tm_hour);
  80.         case 'I':
  81.           ADDN(2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
  82.         case 'M':
  83.           ADDN(2,timeptr->tm_min);
  84.         case 'R':
  85.           ADDS("%H:%M");
  86.         case 'S':
  87.           ADDN(2,timeptr->tm_sec);
  88.         case 'T':
  89.         case 'X':
  90.           ADDS("%H:%M:%S");
  91.         case 'U':
  92.           ADDN(2,(timeptr->tm_yday+7-timeptr->tm_wday)/7);
  93.         case 'W':
  94.           ADDN(2,(timeptr->tm_yday+7-(6+timeptr->tm_wday)%7)/7);
  95.         case 'Y':
  96.           ADDN(4,timeptr->tm_year+1900);
  97.         case 't':
  98.           STOR('\t');
  99.           break;
  100.         case 'n':
  101.           STOR('\n');
  102.           break;
  103.         case '%':
  104.           STOR('%');
  105.           break;
  106.         case '\0':
  107.           format--;
  108.           break;
  109.       }
  110.       size+=tmp;
  111.       s+=tmp;
  112.     }
  113.     else
  114.       STOR(*format);
  115.     format++;
  116.   }
  117.   STOR('\0');
  118.   if(size>maxsize)
  119.   { s-=size;
  120.     if(maxsize) /* Don't know if this is necessary, therefore it's here ;-) */
  121.       s[maxsize-1]='\0';
  122.     size=1;
  123.   }
  124.   return size-1;
  125. }
  126.